/* 
所有使用picker的控件的封装
 */
handlePicker = (mpicker, list, cancel, confirm, title, pid, vid, arrowId, callback) => {
	mpicker = new Picker({
		cancelText: cancel,
		confirmText: confirm,
		title: title,
		data: [list]
	});
	mpicker.on('picker.select', (selectedVal, selectedIndex) => {
		let selectedItem = list[selectedIndex[0]];
		$(`#${vid}`).html(selectedItem.text);
		$(`#${vid}`).val(selectedItem.text);
		$(`#${arrowId}`).css({
			'transform': 'rotate(0deg)'
		});
		callback(selectedItem);
	});
	mpicker.on('picker.cancel', () => {
		$(`#${arrowId}`).css({
			'transform': 'rotate(0deg)'
		});
	});
	return mpicker
}

addSemesterPicker = (pickerId, valueId, arrowId, callback) => {
	let list = getSemesterList().data;
	list.forEach((obj) => {
		obj['text'] = obj.semesterName;
		obj['value'] = obj.semesterId;
		obj['weekBeginDay'] = obj.weekBeginDay;
		if (obj.isCurrentSemester == '1') {
			$(`#${valueId}`).html(obj.semesterName);
			$(`#${valueId}`).val(obj.semesterName);
			callback(obj)
		}
	})

	var pickerSemester = handlePicker(pickerSemester, list, '取消', '确定', '请选择学年学期',
		pickerId, valueId, arrowId, (res) => {
			if (callback) {
				callback(res)
			}
		});
	$(`#${pickerId}`).on('click', () => {
		if (pickerSemester == null) {
			showToast('初始化失败')
		} else if (!list) {
			showToast('暂无数据')
		} else {
			pickerSemester.show();
			$(`#${arrowId}`).css({
				'transform': 'rotate(-180deg)'
			});
		}
	});
}

getSemesterList = () => {
	return {
		"code": 200,
		"data": [{
			"academicYearPk": "2021-2022",
			"semesterCode": "2",
			"semesterName": "2021-2022第二学期",
			"semesterId": "2021-2022-2",
			"isCurrentSemester": "1",
			"weekBeginDay": "7"
		}, {
			"academicYearPk": "2021-2022",
			"semesterCode": "1",
			"semesterName": "2021-2022第一学期",
			"semesterId": "2021-2022-1",
			"isCurrentSemester": "0",
			"weekBeginDay": "1"
		}, {
			"academicYearPk": "2020-2021",
			"semesterCode": "2",
			"semesterName": "2020-2021第二学期",
			"semesterId": "2020-2021-2",
			"isCurrentSemester": "0",
			"weekBeginDay": "7"
		}, {
			"academicYearPk": "2020-2021",
			"semesterCode": "1",
			"semesterName": "2020-2021第一学期",
			"semesterId": "2020-2021-1",
			"isCurrentSemester": "0",
			"weekBeginDay": "7"
		}, {
			"academicYearPk": "2019-2020",
			"semesterCode": "2",
			"semesterName": "2019-2020第二学期",
			"semesterId": "2019-2020-2",
			"isCurrentSemester": "0",
			"weekBeginDay": "7"
		}, {
			"academicYearPk": "2019-2020",
			"semesterCode": "1",
			"semesterName": "2019-2020第一学期",
			"semesterId": "2019-2020-1",
			"isCurrentSemester": "0",
			"weekBeginDay": "7"
		}, {
			"academicYearPk": "2018-2019",
			"semesterCode": "1",
			"semesterName": "2018-2019第一学期",
			"semesterId": "2018-2019-1",
			"isCurrentSemester": "0",
			"weekBeginDay": "7"
		}]
	};
}
